package com.drawm.adt.pseudoCodeEditor {
	import flash.text.StyleSheet;
	import flash.text.TextField;
	import flash.events.Event;
	import flash.display.Sprite;

	/**
	 * @author Drawm
	 */
	public class View extends Sprite {
		private var _model : Model;

		private var _textField:TextField;


		public function View(model:Model) {
			_model = model;
			
			_model.addEventListener(Model.START_APP, onStartApp);
			_model.addEventListener(Model.STOP_APP, onStopApp);
			_model.addEventListener(Model.UPDATE_STYLE, onUpdateStyle);
			
		}

		private function onStopApp(event : Event) : void {
			
//			Stop everything
			_model.removeEventListener(Model.START_APP, onStartApp);
			_model.removeEventListener(Model.STOP_APP, onStopApp);
			_model.removeEventListener(Model.UPDATE_STYLE, onUpdateStyle);
			
			
		}
		

		private function onStartApp(event : Event) : void {
			var padding:int = 4;
			
			
//			Start everything
			_textField = new TextField();
			addChild(_textField);
			_textField.wordWrap = true;
			_textField.width = Model.appWidth - padding;
			_textField.x = padding>>1;
			_textField.height = Model.appHeight - padding;
			_textField.y = padding>>1;
			_textField.border = true;
			_textField.borderColor = 0x888888;
			_textField.multiline = true;
			_textField.selectable = true;
			_textField.htmlText = "<body><kw class=\"trololo\" id=\"torlolo\" >App</kw>{<br/>TEXTE<br/>TEXTE<br/>TEXTE<br/>}</body>";
			
			
			
			_model.addEventListener(Model.RESET_TEXT, onResetText);
		}
		private function onUpdateStyle(evnet:Event):void{
			_textField.styleSheet = Model.styleSheet;
		}
		private function onResetText(event : Event) : void {
			_textField.htmlText = Model.htmlText;
		}
	}
}
